package com.sdry.web.controller.hpf;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import com.sdry.model.hpf.Label;
import com.sdry.model.hpf.Page;
import com.sdry.service.hpf.LabelService;
import com.sdry.utils.ResponseUtil;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
import net.sf.json.JsonConfig;

/** 
* 标签Controller
* @author lian 
* @version 1.0
* @time 2018年10月15日 下午5:14:07
*/

@Controller
@RequestMapping("/labelMana")
public class LabelController {

	@Resource
	private LabelService labelService;  //标签管理业务层
	
	/**
	 * 分页查询标签
	 * @param page 分页条件
	 * @param label 标签
	 * @param response
	 * @throws Exception 
	 */
	@RequestMapping(value="/listPageLabel")
	public void listPageLabel(Page page, Label label, HttpServletResponse response) throws Exception{
		Map<String, Object> map = new HashMap<>();
		map.put("limit", page.getLimit());
		map.put("page", page.getPage());
		if (null != label.getPartName() && !"".equals(label.getPartName())) {
			map.put("partName", label.getPartName());
		} else {
			map.put("partName", "");
			label.setPartName("");
		}
		int count = labelService.countLabel(label);
		List<Label> labelList = labelService.listPageLabel(map);
		JSONObject jsonObject = new JSONObject();
		JsonConfig jsonConfig = new JsonConfig();
		JSONArray jsonArray = JSONArray.fromObject(labelList, jsonConfig);
		jsonObject.put("code", 0);
		jsonObject.put("data", jsonArray);
		jsonObject.put("count", count);
		ResponseUtil.write(response, jsonObject);
	}
	
	/**
	 * 保存标签
	 * @param label 标签
	 * @param response
	 * @throws Exception 
	 */
	@RequestMapping(value="/saveLabel")
	public void saveLabel(Label label, HttpServletResponse response) throws Exception{
		int isSuccess = 0;
		if (label.getRid() == null || "".equals(label.getRid())) {
			isSuccess = labelService.insertLabel(label);
		} else {
			isSuccess = labelService.updateLabel(label);
		}
		ResponseUtil.write(response, isSuccess);
	}
	
	/**
	 * 删除标签
	 * @param str rid组成的字符串
	 * @param response
	 * @throws Exception 
	 */
	@RequestMapping(value="/deleteLabel")
	public void deleteLabel(@RequestParam("str") String str, HttpServletResponse response) throws Exception{
		int isSuccess = labelService.deleteLabel(str);
		ResponseUtil.write(response, isSuccess);
	}
}
